package RegionPartition;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.util.Iterator;
import java.util.TreeMap;

public class RegionPartitionReducer extends Reducer<Text, RegionPartitionFlowBean, Text, RegionPartitionFlowBean> {
    // 定义一个TreeMap作为存储数据的容器（按key排序）
//    private TreeMap<RegionPartitionFlowBean, Text> flowMap = new TreeMap<>();

    //    @Override
    protected void reduce(Text key, Iterable<RegionPartitionFlowBean> values, Reducer.Context context) throws IOException, InterruptedException {
        for (RegionPartitionFlowBean value :
                values) {
            RegionPartitionFlowBean flowBean = new RegionPartitionFlowBean(value.getRegionCode());

            Text idtext = new Text(key);

//             1 向treeMap集合中添加数据
//            flowMap.put(flowBean, idtext);
//            flowMap.put(value, key);
            context.write(idtext, value);
        }
    }

    /*@Override
    protected void cleanup(Reducer.Context context) throws IOException, InterruptedException {
        // 3 遍历集合，输出数据
        Iterator<RegionPartitionFlowBean> iterator = flowMap.keySet().iterator();

        while (iterator.hasNext()) {
            RegionPartitionFlowBean valueOut = iterator.next();
            Text keyOut = flowMap.get(valueOut);

            context.write(keyOut, valueOut);
        }
    }*/
}
